package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author mbg
 * @version 3.5.3
 */
@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 新增菜品
     * @param dishDTO
     */
    @Transactional
    public void savewithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
//        添加菜品
        dishMapper.insert(dish);
//        获取返回添加菜品的主键值
        Long DishId=dish.getId();
        log.info("获取的新增菜品的id是：{}",DishId);
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null&&flavors.size()>0){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(DishId);
            });
            //        向口味表批量插入多条数据
            dishFlavorMapper.insertBatch(flavors);
        }
    }


    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> dishVO=dishMapper.pageQuery(dishPageQueryDTO);
        PageResult pageResult=new PageResult(dishVO.getTotal(),dishVO.getResult());
        return pageResult;
    }

    /**
     * 批量删除菜品
     * @param ids
     */
    public void remove(List<Long> ids) {
//        判断当前菜品能不能被删除   --是否是起售中的菜品
        for (Long id:ids){
            Dish dish=dishMapper.getById(id);
            if (dish.getStatus() == StatusConstant.ENABLE){
//                当前菜品处于起售状态不能被删
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
//        查看此菜品是否能被删除  是否被关联
        List<Long> setmealId= setmealDishMapper.selectsetmealIdByDishId(ids);
        if (setmealId!=null && setmealId.size()>0){
//            改菜品被关联不能被删
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
//          删除菜品表中的数据
        for (Long id:ids){
            dishMapper.deleteById(id);
//            删除关联的口味表
            dishFlavorMapper.deleteByDishId(id);
        }
    }

    /**
     * 根据菜品id查询菜品和口味的数据
     * @param id
     * @return
     */
    public DishVO queryByIdWithFlavor(Long id) {
//        查询菜品数据
        Dish dish=dishMapper.selectById(id);
//        根据菜品id查询口味
        List<DishFlavor> flavors=dishFlavorMapper.selectByDishId(id);
//        将查询到的数据封装到vo
        DishVO dishVO=new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(flavors);
        return dishVO;
    }

    /**
     * 编辑菜品
     * @param dishDTO
     */
    public void updateWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
//        修改菜品表基本信息
        dishMapper.update(dish);
//        删除口味数据
        dishFlavorMapper.deleteByDishId(dishDTO.getId());
//        重新添加口味
        List<DishFlavor> flavors=dishDTO.getFlavors();
        if(flavors!=null&&flavors.size()>0){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dish.getId());
            });
        dishFlavorMapper.insertBatch(flavors);
    }
}

    /**
     * 菜品状态设置
     * @param id
     */
    public void startorStop(Integer status,Long id) {
        Dish dish=new Dish();
        dish.setStatus(status);
        dish.setId(id);
        dishMapper.update(dish);
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.selectByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
